﻿Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Configuration.ConfigurationManager
Partial Class IntraReportTres
    Inherits System.Web.UI.Page
    'instanciar la conexion
    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)

    'listar los clientes
    Private Function Clientes() As DataTable
        Dim da As New SqlDataAdapter(
            "select nom_cliente+' '+ape_cliente as nn,cod_cliente from tb_cliente", cn)
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    'Funcion de pedidos por empleado
    Private Function Pedidos(ByVal cli As String) As DataTable
        Dim da As New SqlDataAdapter(
            "Select * from tb_orden_compra where cod_cliente=@id", cn)
        'parametro
        da.SelectCommand.Parameters.Add("@id", SqlDbType.VarChar).Value = cli

        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'cargar los empleados
            cboClientes1.DataSource = Clientes()
            cboClientes1.DataTextField = "nn"
            cboClientes1.DataValueField = "cod_cliente"
            cboClientes1.DataBind()
        End If
    End Sub

    Protected Sub cboClientes1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboClientes1.SelectedIndexChanged
        'ReportDocument, cargar archivo y datos
        Dim rp As New ReportDocument
        rp.Load(Server.MapPath("rptPedidosCliente.rpt"))
        rp.SetDataSource(Pedidos(cboClientes1.SelectedValue))

        'visor
        CrystalReportViewer1.ReportSource = rp
        CrystalReportViewer1.DataBind()
    End Sub

End Class
